package com.beiding.t;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author 丁常磊
 * @date 2022/4/16 12:10
 */

public class 斜线加数字转文本 {

    public static void main(String[] args) throws UnsupportedEncodingException {

        System.out.println(_txt("\\345\\256\\266\\347\\233\\256\\345\\275\\225\\344\\270\\272"));

    }

    public static String txt_(String t) {

        try {

            String str = URLEncoder.encode(t, "UTF-8");

            String[] s = str.substring(1).split("%");

            StringBuilder s8 = new StringBuilder();

            for (String value : s) {
                s8.append("\\").append(Integer.toOctalString(Integer.valueOf(value, 16)));
            }

            return s8.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    public static String _txt(String t) {

        String[] split = t.split("\\\\");

        StringBuilder builder = new StringBuilder();
        for (String s : split) {
            if (s.length() == 3) {
                String hexString = Integer.toHexString(Integer.valueOf(s, 8));
                builder.append("%").append(hexString);
            }
        }

        try {
            return URLDecoder.decode(builder.toString().toUpperCase(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

    }


}
